package protocol;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;
import org.apache.commons.io.FileUtils;
import utility.Utility;

public class MetaData {
    /**
     * Format metadata : fullpathname(dari folder 8box) file type size
     * lastmodified(milis) checksum
     *
     * @param filename
     */
    private String filePath;
    private String fileChecksum;
    private long lastModified;
    private long fileSize;
    private byte fileType;

    public String getFileChecksum() {
        return fileChecksum;
    }

    public void setFileChecksum(String fileChecksum) {
        this.fileChecksum = fileChecksum;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public long getFileSize() {
        return fileSize;
    }

    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }

    public byte getFileType() {
        return fileType;
    }

    public void setFileType(byte fileType) {
        this.fileType = fileType;
    }

    public long getLastModified() {
        return lastModified;
    }

    public void setLastModified(long lastModified) {
        this.lastModified = lastModified;
    }
    
    public static void PerformMetaData(MetaData md){
        System.out.println("Nama file : " + md.getFilePath());
//        System.out.println("Tipe file : " + md.getFileType());
//        System.out.println("Ukuran file : " + md.getFileSize());
//        System.out.println("Checksum : " + md.getFileChecksum());
        System.out.println("Lastmodified : " + md.getLastModified());
        System.out.println();
    }
    
    public void PerformMetaData(){
        System.out.println("Nama file : " + getFilePath());
        System.out.println("Tipe file : " + getFileType());
        System.out.println("Ukuran file : " + getFileSize());
        System.out.println("Checksum : " + getFileChecksum());
        System.out.println("Lastmodified : " + getLastModified());
        System.out.println();
    }
    
    public MetaData(){
        
    }
    
    public MetaData(MetaData value){
        this.filePath = value.filePath;
        this.fileType = value.fileType;
        this.fileSize = value.fileSize;
        this.fileChecksum = value.fileChecksum;
        this.lastModified = value.lastModified;
    }

    public static MetaData getFileMetaData(String _filePath) {
        MetaData result = new MetaData();
        result.filePath = _filePath;
        //result.lastModified = ;
        try {
            File file = new File(result.filePath);
            if (file.isDirectory()) {
                result.fileChecksum = Utility.convertToSHA1(result.filePath);
                result.fileType = Utility.TYPE_FOLDER;
            } else {
                result.fileChecksum = Utility.convertToSHA1(Utility.getFileByte(result.filePath));
                result.fileType = Utility.TYPE_FILE;
            }
            result.fileSize = file.length();
            result.lastModified = file.lastModified();
        } catch (Exception ex) {
            System.out.println("Metadata failed to create : " + ex);
        }
        return result;
    }

    public static MetaData getFileMetaData(String baseUrl, String _filePath) {
        MetaData result = new MetaData();
        //String fileRelative = _filePath.split(baseUrl)[1];
//        System.out.println("filepath="+_filePath);
        int cut= baseUrl.length()+1;
        //System.out.println("base url="+baseUrl+":"+_filePath);
        //if(baseUrl!="/" || baseUrl!="\\") cut+=1;
        String fileRelative = _filePath.substring(cut);
        //System.out.println("relative = "+fileRelative);
        result.filePath = fileRelative;
        //result.lastModified = ;
        try {
            File file = new File(_filePath);
            if (file.isDirectory()) {
//                result.fileChecksum = Utility.convertToSHA1(_filePath);
                result.fileChecksum = "7";
                result.fileType = Utility.TYPE_FOLDER;
            } else {
//                result.fileChecksum = Utility.convertToSHA1(Utility.getFileByte(_filePath));
                result.fileChecksum = String.valueOf(FileUtils.checksumCRC32(file));
                result.fileType = Utility.TYPE_FILE;
            }
            result.fileSize = file.length();
            result.lastModified = file.lastModified();
        } catch (Exception ex) {
            System.out.println("Metadata failed to create : " + ex);
        }
        return result;
    }

    public static String getMetaDataString(MetaData metaData) {
        String result = "";
        result += "\""+metaData.filePath + "\" ";
        result += metaData.fileType + " ";
        result += metaData.fileSize + " ";
        result += metaData.lastModified + " ";
        result += metaData.fileChecksum;
        System.out.println("message = " + result);
        return result;
    }
    
    public static MetaData readMetaDataString(String metaData){
        MetaData result = new MetaData();
        System.out.println(metaData);
        String[] fileName = metaData.split("\"");
        //String[] fileName2 = fileName[1].split("\"");
        System.out.println(metaData);
       String[] pattern = fileName[2].split(" ");
        if (pattern.length == 5) {
            result.filePath = fileName[1];
            result.fileType = Byte.parseByte(pattern[1]);
            result.fileSize = Long.parseLong(pattern[2]);
            result.lastModified = Long.parseLong(pattern[3]);
            result.fileChecksum = pattern[4].trim();
            System.out.println("file path = " + result.filePath);
            System.out.println("file type = " + result.fileType);
            System.out.println("file size = " + result.fileSize);
            System.out.println("file last modified = " + result.lastModified);
            System.out.println("file checksum = " + result.fileChecksum);
        }
        return result;
    }
    
    public static void createMetadataFile(String url, MetaData metaData) {
        try {
            FileWriter fstream = new FileWriter(url);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(metaData.filePath + "\n");
            out.write(metaData.fileType + "\n");
            out.write(metaData.fileSize + "\n");
            out.write(metaData.lastModified + "\n");
            out.write(metaData.fileChecksum);
            out.close();
            System.out.println("Telah berhasil membuat metadata");
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }

    public static ArrayList<String> readMetaData(String path) throws FileNotFoundException {
        String NL = System.getProperty("line.separator");
        ArrayList<String> result = new ArrayList<String>();
        Scanner scanner = new Scanner(new FileInputStream(path));
        try {
            result.add(scanner.nextLine()); // file path
            result.add(scanner.nextLine()); // file type
            result.add(scanner.nextLine()); // file size
            result.add(scanner.nextLine()); // last modified
            result.add(scanner.nextLine()); // checksum
        } finally {
            scanner.close();
        }
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i));
        }
        return result;
    }

    public static void main(String[] args) {
//        getFileMetaData("D:/8box/client/8box/", "D:/8box/client/8box/~ajax-content-controller.php");
//        readMetaDataString(getMetaDataString(getFileMetaData("D:/test.arff")));
        getMetaDataString(getFileMetaData("D:/8box/client/8box/", "D:/8box/client/8box/repositories/~ajax-content-controller.php"));
    }
}
